﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;

using QuickDev.Common;
using QuickDev.Common.Utils;
using QuickDev.Common.Audit;
using QuickDev.Localization;

using QuickDev.Web.UI.Controls;
using QuickDev.Model;
using QuickDev.Caching;
using QuickDev.BLL;

#endregion

namespace QuickDev.Web.App.Administration.Controls.SiteSettingConfiguration
{
    public class SiteSettingsControl : AdminControl
    {
        #region Constants

        private const string SITE_SETTINGS = "SiteSettings";

        #endregion

        #region Member Variables

        private SiteSettings _siteSettings;
        protected string view;

        #endregion

        #region Page Events

        protected virtual void Page_Load( object sender, EventArgs e )
        {
            view = Utility.GetParameter( "view" );
        }

        #endregion

        #region Properties

        ///// <summary>
        ///// Gets the panel.
        ///// </summary>
        ///// <value>The panel.</value>
        //public Panel Panel
        //{
        //    get
        //    {
        //        return this.FindControl( "pnlSettings" ) as Panel;
        //    }
        //}

        /// <summary>
        /// Gets or sets the site settings.
        /// </summary>
        /// <value>The site settings.</value>
        public SiteSettings SiteSettings
        {
            get
            {
                return _siteSettings;
            }
            set
            {
                _siteSettings = value;
            }
        }

        #endregion

        #region Methods

        #region Public

        #region /*Cannot Be Static*/ Methods

        /// <summary>
        /// Saves the specified site settings.
        /// </summary>
        /// <param name="siteSettings">The site settings.</param>
        /// <returns></returns>
        public ServiceConfiguration SaveConfiguration( SiteSettings siteSettings )
        {
            ServiceConfiguration model = ServiceConfigurationCache.GetModel( SiteSettings.SITESETTINGS );

            Validation.ValidateObjectIsNotNull( siteSettings, SITE_SETTINGS );
            Validation.ValidateObjectType( siteSettings, typeof( SiteSettings ) );

            ServiceConfiguration modelReturn = null;
            if ( model.ConfigurationId > 0 )
            {
                modelReturn = ServiceConfigurationBLL.Update( model.ConfigurationId, SiteSettings.SITESETTINGS, siteSettings, Memberships.CurrentUserId );
                if ( modelReturn != null )
                {
                    QuickDev.Caching.SiteSettingsCache.RemoveSiteSettingsCacheKeyFromCache();
                }
            }
            else
            {
                modelReturn = ServiceConfigurationBLL.Add( SiteSettings.SITESETTINGS, siteSettings, Memberships.CurrentUserId );
                if ( modelReturn != null )
                {
                    QuickDev.Caching.SiteSettingsCache.RemoveSiteSettingsCacheKeyFromCache();
                }
            }

            return modelReturn;
        }

        #endregion

        #endregion

        #endregion
    }
}
